﻿using System;
using System.Web;
using System.Web.SessionState;
using OrderManage.BLL;
using OrderManage.Model;
using OrderManage.Utility;

namespace OrderManage.WebApp.Service
{
    public class SystemManageHandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                var action = RequestStr.GetQueryString("action");
                if (string.IsNullOrEmpty(action)) return;
                var method = GetType().GetMethod(action);
                if (method == null) return;
                object[] parameters = { context };
                method.Invoke(this, parameters);
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }

        #region 登录
        public void Login(HttpContext context)
        {
            var loginCode = context.Request["paramName"];
            var loginPwd = context.Request["paramPwd"];
            if (string.IsNullOrEmpty(loginCode) || string.IsNullOrEmpty(loginPwd))
                context.Response.Write("1");
            if (RequestCheck.CheckKeyWord(loginCode) || RequestCheck.CheckKeyWord(loginPwd))
                context.Response.Write("2");
            else
            {
                var qcmodel = new UserQcInfo
                                  {
                                      UserName = loginCode
                                  };
                var result = new UserBLL().Login(qcmodel);
                if (result.Password == EncryptPassWord.EncryptString(loginPwd))
                {
                    SysContext.CurrentLoginCode = result.UserName;
                    SysContext.CurrentPwd = result.Password;
                    SysContext.CurrentFullName = result.FullName;
                    SysContext.CurrentUserId = result.UserId.ToString();
                    SysContext.CurrentRoleId = result.Role;
                    context.Response.Write("3");

                }
                else
                {
                    context.Response.Write("4");
                }
            }
        }
        #endregion

        #region 加载菜单
        public void LoadMenuList(HttpContext context)
        {
            var roleId = SysContext.CurrentRoleId.ToString();
            if (string.IsNullOrEmpty(roleId)) return;
            var json = new MenuBLL().LoadMenuList(roleId);
            context.Response.Write(json);
            context.Response.End();
        }
        #endregion

        #region 用户管理
        public void GetUserList(HttpContext context)
        {
            var bll = new UserBLL();
            var qcmodel = new UserQcInfo
            {
                UserName = context.Request["UserName"],
                Role = context.Request["Role"],
                FullName = context.Request["FullName"],
                Gender = context.Request["Gender"],
                PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                PageSize = Convert.ToInt32(context.Request["pageSize"]),
                SortField = context.Request["sortField"],
                SortOrder = context.Request["sortOrder"]
            };
            var json = bll.GetUserList(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }
        public void GetUserInfo(HttpContext context)
        {
            var id = context.Request["UserId"];
            if (string.IsNullOrEmpty(id)) return;
            var json = new UserBLL().GetUserInfo(id);
            context.Response.Write(json);
            context.Response.End();
        }
        public void AddUser(HttpContext context)
        {
            var data = context.Request["data"];
            new UserBLL().AddUser(data);
        }
        public void ModifyUser(HttpContext context)
        {
            var data = context.Request["data"];
            new UserBLL().ModifyUser(data);
        }
        public void ModifyPwd(HttpContext context)
        {
            var data = context.Request["data"];
            var json = new UserBLL().ModifyPwd(data);
            context.Response.Write(json);
            context.Response.End();
        }
        public void RemoveUser(HttpContext context)
        {
            var id = context.Request["UserId"];
            if (string.IsNullOrEmpty(id)) return;
            new UserBLL().RemoveUser(id);
        }
        #endregion

        #region 订单管理
        public void GetOrderList(HttpContext context)
        {
            var bll = new OrderBLL();
            var qcmodel = new OrderQcInfo();
            qcmodel.Mobile = context.Request["Mobile"];
            qcmodel.StartDateTime = context.Request["StartDateTime"];
            qcmodel.EndDateTime = context.Request["EndDateTime"];
            qcmodel.Status = context.Request["Status"];
            qcmodel.Role = SysContext.CurrentRoleId.ToString();
            if (qcmodel.Role == "0")
            {
                qcmodel.UserName = SysContext.CurrentLoginCode;
            }
            qcmodel.PageIndex = Convert.ToInt32(context.Request["pageIndex"]);
            qcmodel.PageSize = Convert.ToInt32(context.Request["pageSize"]);
            qcmodel.SortField = context.Request["sortField"];
            qcmodel.SortOrder = context.Request["sortOrder"];
            var json = bll.GetOrderList(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }
        public void AddOrder(HttpContext context)
        {
            var userName = SysContext.CurrentLoginCode;
            if (string.IsNullOrEmpty(userName)) return;
            var data = context.Request["data"];
            new OrderBLL().AddOrder(userName, data);
        }
        public void RemoveOrder(HttpContext context)
        {
            var id = context.Request["OrderId"];
            if (string.IsNullOrEmpty(id)) return;
            new OrderBLL().RemoveOrder(int.Parse(id));
        }
        public void GetMobileInfo(HttpContext context)
        {
            var mobileCode = context.Request["MobileCode"];
            if (string.IsNullOrEmpty(mobileCode)) return;
            var mobileStr =
            new MobileCodeWSRef.MobileCodeWS().getMobileCodeInfo(mobileCode, null);
            var arr = mobileStr.Split('：');
            var info = arr[1];
            context.Response.Write(info);
            context.Response.End();
        }
        public void GetTotalAmount(HttpContext context)
        {
            var qcmodel = new OrderQcInfo();
            qcmodel.Mobile = context.Request["Mobile"];
            qcmodel.StartDateTime = context.Request["StartDateTime"];
            qcmodel.EndDateTime = context.Request["EndDateTime"];
            qcmodel.Status = context.Request["Status"];
            qcmodel.Role = SysContext.CurrentRoleId.ToString();
            if (qcmodel.Role == "0")
            {
                qcmodel.UserName = SysContext.CurrentLoginCode;
            }
            var json = new OrderBLL().GetTotalAmount(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }
        public void GetTotalRechargeAmountChartList(HttpContext context)
        {
            var bll = new OrderBLL();
            var year = context.Request["Year"];
            if (string.IsNullOrEmpty(year)) return;
            var json = bll.GetTotalRechargeAmountChartList(year);
            context.Response.Write(json);
            context.Response.End();
        }

        #endregion

        public void CheckedCode(HttpContext context)
        {
            var tableName = context.Request["tableName"];
            var fieldName = context.Request["fieldName"];
            var code = context.Request["Code"];
            var json = new CommonBLL().CheckedCode(tableName, fieldName, code);
            context.Response.Write(json);
            context.Response.End();
        }
        public void CheckPermissions(HttpContext context)
        {
            var role = SysContext.CurrentRoleId;
            context.Response.Write(role);
            context.Response.End();
        }

        public void UserNameValidation(HttpContext context)
        {
            var userName = context.Request["UserName"];
            if (string.IsNullOrEmpty(userName)) return;
            if (RequestCheck.CheckKeyWord(userName))
            {
                context.Response.Write(100);
                context.Response.End();
                return;
            }
            var result = new UserBLL().UserNameValidation(userName);
            if (!result)
            {
                context.Response.Write(200);
                context.Response.End();
            }
        }
        public void PasswordValidation(HttpContext context)
        {
            var userName = context.Request["UserName"];
            var password = context.Request["Password"];
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)) return;
            if (RequestCheck.CheckKeyWord(userName) || RequestCheck.CheckKeyWord(password))
            {
                context.Response.Write(100);
                context.Response.End();
                return;
            }
            var result = new UserBLL().PasswordValidation(userName, password);
            if (!result)
            {
                context.Response.Write(200);
                context.Response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
